#include "compute.h"
#include "ui_compute.h"
#include "exprfactory.h"

#include <QDebug>

Compute::Compute(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Compute)
{
    ui->setupUi(this);
    this->tmp = "";
    //
}

Compute::~Compute()
{
    delete ui;
}


void Compute::on_btn_0_clicked()
{
    //按0
    if(tmp != "")
    {
        tmp.append(ui->btn_0->text()); //获取要显示的值
        //显示
        ui->lcd_display->display(tmp);
    }

}

void Compute::on_btn_1_clicked()
{
    tmp.append(ui->btn_1->text());
    ui->lcd_display->display(tmp);
}


void Compute::on_btn_2_clicked()
{
    tmp.append(ui->btn_2->text());
    ui->lcd_display->display(tmp);
}

void Compute::on_btn_3_clicked()
{
    tmp.append(ui->btn_3->text());
    ui->lcd_display->display(tmp);
}

void Compute::on_btn_4_clicked()
{
    tmp.append(ui->btn_4->text());
    ui->lcd_display->display(tmp);
}

void Compute::on_btn_5_clicked()
{
    tmp.append(ui->btn_5->text());
    ui->lcd_display->display(tmp);
}

void Compute::on_btn_6_clicked()
{
    tmp.append(ui->btn_6->text());
    ui->lcd_display->display(tmp);
}

void Compute::on_btn_7_clicked()
{
    tmp.append(ui->btn_7->text());
    ui->lcd_display->display(tmp);
}

void Compute::on_btn_8_clicked()
{
    tmp.append(ui->btn_8->text());
    ui->lcd_display->display(tmp);
}

void Compute::on_btn_9_clicked()
{
    tmp.append(ui->btn_9->text());
    ui->lcd_display->display(tmp);
}

void Compute::on_btn_sum_clicked()
{
    this->oper = ExprFactory::createOperation('+');
    this->oper->setNum_1(tmp.toInt());
    this->tmp = "";

}

void Compute::on_btn_sub_clicked()
{
    this->oper = ExprFactory::createOperation('-');
    this->oper->setNum_1(tmp.toInt());
    this->tmp = "";
}

void Compute::on_btn_mul_clicked()
{
    this->oper = ExprFactory::createOperation('*');
    this->oper->setNum_1(tmp.toInt());
    this->tmp = "";
}

void Compute::on_btn_div_clicked()
{
    this->oper = ExprFactory::createOperation('/');
    this->oper->setNum_1(tmp.toInt());
    this->tmp = "";
}

void Compute::on_btn_equal_clicked()
{
    if(oper!=nullptr)
    {
        this->oper->setNum_2(tmp.toInt());
        //qDebug()<<oper->getNum_1()<<","<<oper->getNum_2()<<endl;
        //qDebug()<<oper->getResult();
        this->ui->lcd_display->display(oper->getResult());
        tmp = QString::number(oper->getResult());
    }

}

void Compute::on_btn_c_clicked()
{
    this->tmp = "";
    ui->lcd_display->display("0");
}
